package com.abe.libcore.base.screen.j;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.widget.Toast;

import com.abe.libcore.base.screen.OnHandlerListener;

public abstract class AbeJActivity extends BJScreenActivity {
    private final static int appToast = 0x999;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutRes());
        initContent();
    }

    protected abstract int getLayoutRes();

    protected abstract void initContent();

    /**********************************************************************************************/
    protected Context getContext() {
        return this;
    }

    protected void gotoActivity(Class<?> act) {
        startActivity(getIntent(act));
    }

    protected void gotoActivity(Class<?> act, Bundle extras) {
        Intent intent = getIntent(act);
        intent.putExtras(extras);
        startActivity(intent);
    }

    protected void gotoActivityForResult(Class<?> act, int code) {
        startActivityForResult(getIntent(act), code);
    }

    protected void gotoActivityForResult(Class<?> act, Bundle extras, int code) {
        Intent intent = getIntent(act);
        intent.putExtras(extras);
        startActivityForResult(intent, code);
    }

    private Intent getIntent(Class<?> act) {
        return new Intent(this, act);
    }

    /**********************************************************************************************/
    protected class BaseHandler extends Handler {
        BaseHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == appToast) {
                Toast.makeText(getContext(), msg.obj.toString(), Toast.LENGTH_SHORT).show();
            } else {
                if (isImp(AbeJActivity.class, OnHandlerListener.class)) {
                    ((OnHandlerListener) AbeJActivity.this).dispatchHandleMessage(msg);
                }
            }
        }
    }

    protected void toast(String content) {
        if (TextUtils.isEmpty(content)) return;
        localSendMsg(appToast, content);
    }

    protected void localSendMsg(int what, Object obj) {
        getHandler().sendMessage(getMessage(what, obj));
    }

    //获取Message
    protected Message getMessage(int what, Object obj) {
        Message msg = new Message();
        msg.what = what;
        msg.obj = obj;
        return msg;
    }

    //获取Handler
    protected BaseHandler getHandler() {
        return new BaseHandler(Looper.getMainLooper());
    }

    /**
     * 是否实现接口
     *
     * @param a 类
     * @param b 接口
     */
    protected Boolean isImp(Class<?> a, Class<?> b) {
        return b.isAssignableFrom(a);
    }
}